
package com.wyzz.hr.bo.impl;

import com.wyzz.hr.base.FileOperate;
import com.wyzz.hr.bo.IEmpTrainHisBo;
import com.wyzz.hr.dao.IEmpTrainHisDao;
import com.wyzz.hr.pojo.Emphistorytrain;
import com.wyzz.hr.pojo.Employee;

import java.util.List;
import org.hibernate.FetchMode;
import org.hibernate.criterion.*;


public class EmpTrainHisBoImpl implements IEmpTrainHisBo {

	public EmpTrainHisBoImpl() {
	}

	public String insert(Emphistorytrain emphis) {
		empTrainHisDao.saveObject(emphis);
		return emphis.getEhtId();
	}

	public List search(String employeeId) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Emphistorytrain.class);
		detachedCriteria.setFetchMode(Emphistorytrain.PROP_EMPLOYEE,
				FetchMode.DEFAULT);
		detachedCriteria.add(Restrictions.eq((new StringBuilder()).append(
				Emphistorytrain.PROP_EMPLOYEE).append(".").append(
				Employee.PROP_ID).toString(), employeeId));
		detachedCriteria.addOrder(Order
				.desc(Emphistorytrain.PROP_EHT_START_DATE));
		List result = empTrainHisDao.findByCriteria(detachedCriteria);
		return result;
	}

	public void delete(String ehtId) {
		String hql = (new StringBuilder()).append(
				"delete from Emphistorytrain where ehtId='").append(ehtId)
				.append("'").toString();
		empTrainHisDao.exeHql(hql);
	}

	public void update(Emphistorytrain emphis) {
		empTrainHisDao.saveOrupdate(emphis);
	}

	public List search(Emphistorytrain empHis) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Emphistorytrain.class);
		detachedCriteria.setFetchMode(Emphistorytrain.PROP_EMPLOYEE,
				FetchMode.DEFAULT);
		detachedCriteria.add(Restrictions.eq(Emphistorytrain.PROP_EHT_ID,
				empHis.getEhtId()));
		detachedCriteria.addOrder(Order
				.desc(Emphistorytrain.PROP_EHT_START_DATE));
		List emphisObj = empTrainHisDao.findByCriteria(detachedCriteria);
		return emphisObj;
	}

	public boolean deleteAttach(String contractId, String fileName) {
		try {
			FileOperate.deleteFile("sys.profile.file.path", fileName);
			String hql = (new StringBuilder())
					.append(
							"update Emphistorytrain set ehtAttatchment=null where ehtId='")
					.append(contractId).append("'").toString();
			empTrainHisDao.exeHql(hql);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public IEmpTrainHisDao getEmpTrainHisDao() {
		return empTrainHisDao;
	}

	public void setEmpTrainHisDao(IEmpTrainHisDao empTrainHisDao) {
		this.empTrainHisDao = empTrainHisDao;
	}

	public Emphistorytrain load(String ehtId, String fetches[]) {
		return (Emphistorytrain) empTrainHisDao.loadObject(
				com.wyzz.hr.pojo.Emphistorytrain.class, ehtId, fetches,
				new boolean[0]);
	}

	private IEmpTrainHisDao empTrainHisDao;
}
